const models = require('../db/mysqldb/index')
const { Op } = require("sequelize");

class DataAPIModel {
  /**
   * @description 根据id和name查找API
   * @param {string} data_api_id 
   * @param {string} name 
   * @returns 
   */
  static async findByIdAndName (data_api_id, name) {
    return await models.data_api.findOne({
      where: {
        name,
        delete_timestamp: {
          [Op.is]: null,
        },
        data_api_id: {
          [Op.ne]: data_api_id,
        }
      }
    })
  }
  /**
   * @description 根据id查找API
   * @param {string} data_api_id 
   * @returns 
   */
  static async findById (data_api_id) {
    return await models.data_api.findOne({
      where: {
        data_api_id,
        delete_timestamp: {
          [Op.is]: null,
        }
      }
    })
  }
  /**
   * @description 根据name查找API
   * @param {string} data_api_id 
   * @returns 
   */
  static async findByName (name) {
    return await models.data_api.findOne({
      where: {
        name,
        delete_timestamp: {
          [Op.is]: null,
        }
      }
    })
  }
  /**
   * @description 创建
   * @param {string} name 
   * @param {string} server 
   * @param {string} methods 
   * @param {string} remark 
   * @param {string} Params 
   * @param {string} Headers 
   * @param {string} Body 
   * @returns 
   */
  static async create (name, server, methods, remark, Params, Headers, Body, op_user_id,) {
    return await models.data_api.create({
      name, server, methods, remark, Params, Headers, Body,
      create_by: op_user_id
    })
  }
  /**
   * @description 更新api
   * @param {string} data_api_id 
   * @param {string} name 
   * @param {string} server 
   * @param {string} methods 
   * @param {string} remark 
   * @param {string} Params 
   * @param {string} Headers 
   * @param {string} Body 
   * @param {*} op_user_id 
   * @returns 
   */
  static async update (data_api_id, name, server, methods, remark, Params, Headers, Body, op_user_id,) {
    return await models.data_api.update(
      {
        name, server, methods, remark, Params, Headers, Body,
        update_by: op_user_id,
        update_timestamp: Date.now()
      },
      {
        where: { data_api_id },
      }
    )
  }
  static async delete (data_api_id, op_user_id) {
    return await models.data_api.update(
      {
        delete_timestamp: Date.now(),
        delete_by: op_user_id
      },
      {
        where: { data_api_id },
      }
    )
  }
  /**
   * @description 获取api列表数据
   * @param {*} limit 
   * @param {*} size 
   * @param {*} name 
   * @param {*} server 
   * @param {*} methods 
   * @param {*} remark 
   * @returns 
   */
  static async list (limit, size = 1, name, server, methods, remark,) {
    let sql = `select SQL_CALC_FOUND_ROWS data_api_id,name, server, methods, remark, Params, Headers, Body,update_by,update_timestamp,create_by, create_timestamp from data_api where 1 = 1 AND delete_timestamp IS NULL `
    if (name) {
      sql = sql + `AND name like '%${name}%' `
    }
    if (server) {
      sql = sql + `AND server like '%${server}%' `
    }
    if (methods) {
      sql = sql + `AND methods = '${methods}' `
    }
    if (remark) {
      sql = sql + `AND remark like '%${remark}%' `
    }
    sql = sql + "ORDER BY create_timestamp DESC,update_timestamp DESC "
    if (limit) {
      sql = sql + `LIMIT ${limit} OFFSET  ${(size - 1) * limit} `
    }
    const result = await models.sequelize.query(sql, { replacements: ['active'], type: models.sequelize.QueryTypes.SELECT })
    const count = await models.sequelize.query(`SELECT FOUND_ROWS() as count;`, { replacements: ['active'], type: models.sequelize.QueryTypes.SELECT })
    return { result, count }
  }
  static async all () {
    let sql = `select data_api_id as value,name as label from data_api where 1 = 1 AND delete_timestamp IS NULL `
    return await models.sequelize.query(sql, { replacements: ['active'], type: models.sequelize.QueryTypes.SELECT })
  }
}
module.exports = {
  DataAPIModel,
}
